package com.cch.demo.bus.service.impl;

import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Inport;
import com.cch.demo.bus.mapper.InportMapper;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.InportService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 华哥
 * @since 2020-03-12
 */
@Service
@Transactional
@DS("master")
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService {

    @Resource
    private GoodsService goodsServiceImpl;
    @Override
    public boolean save(Inport entity) {
        //根据商品编号查询商品
        Goods goods = goodsServiceImpl.getById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()+entity.getNumber());
        goodsServiceImpl.updateById(goods);
        //保存进货信息
        return super.save(entity);
    }

    @Override
    public boolean updateById(Inport entity) {
        Inport inport = this.getBaseMapper().selectById(entity.getId());
        Goods goods = goodsServiceImpl.getById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()-inport.getNumber()+entity.getNumber());
        goodsServiceImpl.updateById(goods);

        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {

        Inport inport = this.getBaseMapper().selectById(id);
        Goods goods = goodsServiceImpl.getById(inport.getGoodsid());

        goods.setNumber(goods.getNumber()-inport.getNumber());
        goodsServiceImpl.updateById(goods);
        return super.removeById(id);
    }

	@Override
	@DS("#DSname")
	public IPage<Inport> page(String DSname, IPage<Inport> page, Wrapper<Inport> queryWrapper) {
		return super.page(page,queryWrapper);
	}
}
